﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace UserInterface
{
    /// <summary>
    /// Enables saving and loading of objects to and from files
    /// </summary>
    public static class Serializer
    {
        /// <summary>
        /// Creates a new file of a single object
        /// </summary>
        /// <param name="filename">the name of the file to create</param>
        /// <param name="objectToSerialize">Serialized object to save in file</param>
        public static void SerializeObject(string filename, object objectToSerialize)
        {
            Stream stream = File.Open(filename, FileMode.Create, FileAccess.Write);
            BinaryFormatter bFormatter = new BinaryFormatter();
            bFormatter.Serialize(stream, objectToSerialize);
            stream.Close();
        }

        /// <summary>
        /// Deserializes and loads a single object from a file
        /// </summary>
        /// <param name="filename">the filename of the file that holds the object</param>
        /// <returns></returns>
        public static object DeSerializeObject(string filename)
        {
            object objectToDeSerialize;
            Stream stream = File.Open(filename, FileMode.Open);
            BinaryFormatter bFormatter = new BinaryFormatter();
            objectToDeSerialize = bFormatter.Deserialize(stream);
            stream.Close();
            return objectToDeSerialize;
        }
    }
}
